package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ShangjuansRBody struct {
	Data       []models.Shangjuan `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

func Shangjuans(ctx *gin.Context) {
	request := NiudansBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Shangjuan
	res := data_source.Db.Where("status = 1")
	res.Model(&models.Shangjuan{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShangjuansRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type ShangjuanBody struct {
	ID        int64   `json:"id" gorm:"id"`
	Daibi     float64 `json:"daibi" gorm:"daibi"`         // 赏卷完全分享获得代笔
	Num       int64   `json:"num" gorm:"num"`             // 需要分享几个人
	Userdaibi float64 `json:"userdaibi" gorm:"userdaibi"` // 每个人分享获得矿石数
	Name      string  `json:"name" gorm:"name"`
	Image     string  `json:"image" gorm:"image"`
	Statsu    int64   `json:"statsu" gorm:"statsu"`
	Day       int64   `json:"day" gorm:"day"` // 领取后多久到期 0不限
}

func Shangjuanadd(ctx *gin.Context) {
	request := ShangjuanBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	image := request.Image
	daibi := request.Daibi
	num := request.Num
	userdaibi := request.Userdaibi
	day := request.Day

	gt := models.Shangjuan{
		Name:      name,
		Image:     image,
		Status:    1,
		Daibi:     daibi,
		Num:       num,
		Userdaibi: userdaibi,
		Day:       day,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Shangjuanupdate(ctx *gin.Context) {
	request := ShangjuanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	image := request.Image
	daibi := request.Daibi
	num := request.Num
	userdaibi := request.Userdaibi
	day := request.Day

	gt := models.Shangjuan{
		Name:      name,
		Image:     image,
		Status:    1,
		Daibi:     daibi,
		Num:       num,
		Userdaibi: userdaibi,
		Day:       day,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Shangjuandel(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Model(&models.Shangjuan{}).Where("id = ?", id).Update("status", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
